#ifndef ENDIAN_HEADER
#define ENDIAN_HEADER

// this header needs
#include "BASIC_TYPES.h"

// ENUM WITH SUPPORTED ENDIANNESS
typedef enum
{
  ENDIAN_UNKNOWN, // no endian (not supported)
  ENDIAN_BIG,     // big endian mode
  ENDIAN_LITTLE,  // little endian mode
} endian_type;

// initialized to that for now, it is mandatory
// to execute the CHECK_SYS_ENDIAN() funtion
// for all the other functions in this library
// to execute correctly
endian_type SYS_ENDIAN = ENDIAN_UNKNOWN;

// var_endian enum
// the third variable types enum
typedef enum
{
  VAR_ENDIAN_TYPE = VAR_MEM_SPACE_ERR + 1,
} var_endian;

// functions
void CHECK_SYS_ENDIAN(void);

// CHECK_SYS_ENDIAN() function
// function to check the system current endianness.
// Can only consider BIG and LITTLE endian systems as of now
void CHECK_SYS_ENDIAN(void)
{
  umax test = 0x00112233;
  // I need at least a 4 byte long umax variable otherwise end program execution
  if (UMAX_SIZE < 4) {
    printf("UMAX_ERR: UMAX INT TYPE TOO TINY. TERMINATING PROGRAM.\n");
    exit(EXIT_FAILURE);
  }
  
  byte * ptr = (byte *) &test;
  if (ptr[UMAX_SIZE - 1] == 0x33
      && ptr[UMAX_SIZE - 2] == 0x22
      && ptr[UMAX_SIZE - 3] == 0x11
      && ptr[UMAX_SIZE - 4] == 0x00)
    SYS_ENDIAN = ENDIAN_BIG;
  else if (ptr[0] == 0x33 && ptr[1] == 0x22 && ptr[2] == 0x11 && ptr[3] == 0x00)
    SYS_ENDIAN = ENDIAN_LITTLE;
  else /* wont allow program execution on an unknown endian system */ {
    printf("ENDIAN_ERR: UKNOWN SYSTEM ENDIAN. TERMINATING PROGRAM.\n");
    exit(EXIT_FAILURE);
  }
}

#endif // ENDIAN_HEADER
